<?php return; // This trest case is deprecated!!!
require_once('lanceur_spip.php');
require_once(_DIR_PLUGIN_PDK.'classes/pdk_model.php');
require_once(_DIR_PLUGIN_PDK.'classes/product_model.php');

class testClassProduct_model extends SpipTest {
    
    /**
     *  Run before each test
     */
    function setUp() {
        $this->clearTables();
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        
    }
    
    function __destruct() {
        //$this->clearTables();
    }
    
    function clearTables() {
        // Clear tables:
        sql_query('TRUNCATE TABLE spip_pdk_products');
        sql_query('TRUNCATE TABLE spip_pdk_products_descriptions');
        sql_query('TRUNCATE TABLE spip_pdk_categories');
    }
    
    /**
     *  Простой тест фильтра данных
     */
    function testDataFilter_Simple() {
        $data = array('title'=>'Product1','prix'=>666, 'price'=>777,
                      'short_description'=>'Short description',
                      'full_desc'=>'Full description',
                      'full_description'=>'Real full description');
        $product = new Product_model();
        $filtered_data = $product->form_data_from_array($data);
        $this->assertEqual($filtered_data, array('title'=>$data['title'],
                                                 'price'=>$data['price'],
                                                 'short_description'=>$data['short_description'],
                                                 'full_description'=>$data['full_description']
                                                 ));
        
    }
    
    /**
     *  Фильтр данных не должен вернуть данных таблицы spip_pdk_categories,
     *  так как она не является hard_join
     */
    function testDataFilter_HardJoinDataMustNotExist() {
        $data = array('title'=>'Product1',
                      'short_desc'=>'Short description',
                      'category_title'=>'Category title'
                      );
        $product = new Product_model();
        $filtered_data = $product->form_data_from_array($data);        
        $this->assertNull($filtered_data['category_title']);
    }
    
    /**
     *  Делаем таблицу spip_pdk_categories hard_join, фильтр должен вернуть данные, соответствующие это таблице
     */
    function testDataFilter_HardJoinDataMustExistIfParameterSet() {
        $data = array('title'=>'Product1',
                      'short_desc'=>'Short description',
                      'category_title'=>'Category title'
                      );
        $product = new Product_model();
        $filtered_data = $product->form_data_from_array($data, false);        
        $this->assertEqual($filtered_data['category_title'], $data['category_title']);
    }
    
    function testInsertFields_DataWithID() {        
        $product = new Product_model();
        $data = array('id_product'=>1,'title'=>'Product1','price'=>666,
                      'short_description'=>'Short description',
                      'full_description'=>'Full description','id_category'=>1);
        $product->insert($data);
        // Check table "spip_pdk_products"
        $prd = sql_fetsel('*','spip_pdk_products','id_product=1');
        $this->assertEqual($prd, array('id_product'=>$data['id_product'],
                                       'titre'=>$data['title'],
                                       'prix'=>$data['price'],
                                       'id_category'=>1));
        // Check table "spip_pdk_products_descriptions"
        $prd_desc = sql_fetsel('*','spip_pdk_products_descriptions','id_product=1');
        $this->assertEqual($prd_desc, array('id_product'=>$data['id_product'],
                                            'short_desc'=>$data['short_description'],
                                            'full_desc'=>$data['full_description']));        
	}		
        
	function testInsertCount_DataWithoutID() {	    
        $product = new Product_model();
        $data = array('title'=>'Product1','price'=>666,
                      'short_description'=>'Short description',
                      'full_description'=>'Full description');
        $product->insert($data);        
        $count_prd = sql_countsel('spip_pdk_products');                      
        $count_prd_desc = sql_countsel('spip_pdk_products_descriptions');
        // There is only one row in tables:
        $this->assertEqual(1, $count_prd);
        $this->assertEqual(1, $count_prd_desc);
	}
    
    /**
     *  При вставке продукта в табличку spip_pdk_categories ничего не должно вставляться,
     *  так как эта табличка не является hard_join  
     */
    function testInsertCategoriesUntouched() {
        $product = new Product_model();
        $data = array('title'=>'Product1','price'=>666,
                      'short_description'=>'Short description',
                      'full_description'=>'Full description',
                      'category_title'=>'Category title',
                      'category_description'=>'Сategory description');
        $product->insert($data);
        $count_cat = sql_countsel('spip_pdk_categories');
        $this->assertEqual(0, $count_cat);
    }
    
    
    /**
     *  Test get_total method
     */
    function testGetTotal() {        
        $product = new Product_model();
        $data = array('title'=>'Product','price'=>666,'short_description');
        $product->insert($data);
        $product->insert($data);
        $product->insert($data);
        $this->assertEqual(3, $product->get_total());
    }
    
    /**
     *  Categories table is empty. Despite this one product must be selected,
     *  because we have LEFT JOIN on categories
     */
    function testSelectOneProduct_LeftJoinOnCategories() {
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        
        $product = new Product_model();        
        $sel_count = count($product->select());
        $this->assertEqual(1,$sel_count);
    }
    
    /**
     *  Products descriptions table is empty. According to this no products must be selected,
     *  because we have INNER JOIN on products descriptions
     */
    function testSelectOneProduct_InnerJoinOnProductsDescriptions() {
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        
        $product = new Product_model();        
        $sel_count = count($product->select());
        $this->assertEqual(0,$sel_count);
    }

    /**
     *  Check selected data by id Product 
     */
    function testSelectProductByID_CheckData() {
        $data = array('id_product'=>1,'title'=>'Product', 'price'=>'999','id_category'=>1,
                      'short_description'=>'Short Description', 'full_description'=>'Full Description',
                      'category_title'=>'Category Title', 'category_description'=>'Category Description');
        sql_insertq('spip_pdk_products', array('id_product'=>$data['id_product'],
                                               'titre'=>$data['title'], 
                                               'prix'=>$data['price'],
                                               'id_category'=>$data['id_category']));
        sql_insertq('spip_pdk_products_descriptions',array('id_product'=>$data['id_product'],
                                                           'short_desc'=>$data['short_description'],
                                                           'full_desc'=>$data['full_description']));
        sql_insertq('spip_pdk_categories',array('id_category'=>$data['id_category'],
                                                         'titre'=>$data['category_title'],
                                                         'description'=>$data['category_description']));
        
        $product = new Product_model();        
        $sel_data = $product->select_by_id(1);        
        $this->assertEqual($data, $sel_data);                                                 
    }
    
    /**
     *  Let's check if couple of products will be selected.  
     */
    function testSelectCoupeOfProducts() {
        // Product with description and category - must be selected
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_categories',array('id_category'=>1,'titre'=>'Category Title', 'description'=>'Category Description'));
        
        // Product only with description - must be selected
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>2));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        
        // Product without description and category
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        
        $product = new Product_model();        
        $sel_count = count($product->select());
        $this->assertEqual(2,$sel_count);
    }
    
    /**
     *  If products are deleted, data from spip_pdk_products and spip_pdk_products_descriptions 
     *  tables must be deleted but category must left, because it isn't hard joined
     */
    function testDeleteProducts_CategoryMustLeft() {
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_categories',array('id_category'=>1,'titre'=>'Category Title', 'description'=>'Category Description'));
        
        $product = new Product_model();
        $product->delete();
        $this->assertEqual(0, sql_countsel('spip_pdk_products'));
        $this->assertEqual(0, sql_countsel('spip_pdk_products_descriptions'));
        $this->assertEqual(1, sql_countsel('spip_pdk_categories'));
    }
    
    /**
     *  Delete product by ID. Category must left.
     */
    function testDeleteProductByID_CategoryMustLeft() {
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_categories',array('id_category'=>1,'titre'=>'Category Title', 'description'=>'Category Description'));
        $product = new Product_model();
        $product->delete_by_id(1); 
        $this->assertEqual(0, sql_countsel('spip_pdk_products'));
        $this->assertEqual(0, sql_countsel('spip_pdk_products_descriptions'));
        $this->assertEqual(1, sql_countsel('spip_pdk_categories'));
    }
    
    /**
     *  When updating products category must left untouched 
     */
    function testUpdateProducts_CategoryMustLeftUntouched() {
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1,'prix'=>'999'));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1,'prix'=>'999'));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_categories',array('id_category'=>1,'titre'=>'Category Title', 'description'=>'Category Description'));
        
        $update_data = array('price'=>666, 'short_description'=>'Short Description Updated', 
                             'category_title'=>'Category Title Updated');
        $product = new Product_model();
        $product->update('prix=999', $update_data);
        
        $products = sql_fetch_all(sql_select('prix', 'spip_pdk_products'));
        $this->assertEqual($update_data['price'],$products[0]['prix']);
        $this->assertEqual($update_data['price'],$products[1]['prix']);
        
        $products_descs = sql_fetch_all(sql_select('short_desc', 'spip_pdk_products_descriptions'));
        $this->assertEqual($update_data['short_description'],$products_descs[0]['short_desc']);
        $this->assertEqual($update_data['short_description'],$products_descs[1]['short_desc']);
        
        $products_descs = sql_fetch_all(sql_select('titre', 'spip_pdk_categories'));
        $this->assertNotEqual($update_data['category_title'], $products_descs[0]['titre']);
    }
    
    /**
     *  When updating product by ID category must left untouched 
     */
    function testUpdateByID_CategoryMustLeftUntouched() {
        sql_insertq('spip_pdk_products', array('titre'=>'Product','id_category'=>1,'prix'=>'999'));
        sql_insertq('spip_pdk_products_descriptions',array('short_desc'=>'Short Description'));
        sql_insertq('spip_pdk_categories',array('id_category'=>1,'titre'=>'Category Title', 'description'=>'Category Description'));
        
        $update_data = array('price'=>666, 'short_description'=>'Short Description Updated', 
                             'category_title'=>'Category Title Updated');
        $product = new Product_model();
        $product->update_by_id(1, $update_data);
        
        $products = sql_fetch_all(sql_select('prix', 'spip_pdk_products'));
        $this->assertEqual($update_data['price'], $products[0]['prix']);
        
        $products_descs = sql_fetch_all(sql_select('short_desc', 'spip_pdk_products_descriptions'));
        $this->assertEqual($update_data['short_description'],$products_descs[0]['short_desc']);
        
        $products_descs = sql_fetch_all(sql_select('titre', 'spip_pdk_categories'));
        $this->assertNotEqual($update_data['category_title'], $products_descs[0]['titre']);
    }
    
    /**
     *  Test function get_empty
     */
    function testGetEmpty() {
        $data = array('id_product'=>'', 'titre'=>'', 'title'=>'', 'prix'=>'','price'=>'',
                      'id_category'=>'', 'short_desc'=>'', 'short_description'=>'', 'full_desc'=>'','full_description'=>'',
                      'category_title'=>'', 'description'=>'', 'category_description'=>'');
        $product = new Product_model();
        $empty_data = $product->get_empty();
        $this->assertEqual($data, $empty_data);
    }
}